package com.xunsiya.interceptor;

import com.xunsiya.bean.ResultData;
import com.xunsiya.common.config.UploadConfig;
import com.xunsiya.common.config.util.ConstantInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * 全局异常拦截
 *
 * @author panjun
 * @version 1.0
 *          <p>
 *          <p>修订人		修订时间			描述信息
 *          <p>-----------------------------------------------------
 *          <p>panjun		2018/12/4		初始创建
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    private static final Logger logger =
            LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Autowired
    private UploadConfig uploadConfig;

    /**
     * 处理上传异常
     *
     * @param t
     * @return
     */
    @ExceptionHandler(MultipartException.class)
    public ResultData handleAll(Throwable t) throws Exception {

        logger.error("=>" + t.getMessage());
        return new ResultData(false, ConstantInfo.HttpCode.HTTP_501.getValue(), ConstantInfo.HttpCode.HTTP_501.getDesc() + uploadConfig.getMaxFileSize());

    }
}
